package com.dindang.checkline.mvp.ui.activity.adapter;

import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.dindang.checkline.R;
import com.dindang.checkline.app.utils.TextTransformUtils;
import com.dindang.checkline.mvp.model.api.Api;
import com.dindang.checkline.mvp.model.entity.TaskDetail;
import com.longxi.gamer_base.utils.DateTimeUtils;

import java.util.Date;
import java.util.List;

/**评论 数据适配器*/
public class ProgressViewAdapter extends RecyclerView.Adapter<ProgressViewAdapter.ViewHolder> {
    List<TaskDetail.EventBean> list;
    public ProgressViewAdapter(List<TaskDetail.EventBean> list) {
        this.list = list;
    }
    private String reason;
    String[] states = {"", "等待接单", "等待开始", "正在巡检", "正常结束", "异常结束", "逾期终止", "逾期未巡", "逾期结束"};

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.taskdetail_item_progress, parent, false);
        ProgressViewAdapter.ViewHolder viewHolder = new ProgressViewAdapter.ViewHolder(view);
        return viewHolder;
    }

    public void setReason(String reason){
        this.reason = reason;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        TaskDetail.EventBean flow = list.get(position);


            if(position == 0&&flow.getNext_status()!=4&&flow.getNext_status()!=5){//第一个状态为红色
                holder.tv_state.setTextColor(holder.tv_state.getContext().getResources().getColor(R.color.red_bg));
            }else{
                holder.tv_state.setTextColor(holder.tv_state.getContext().getResources().getColor(R.color.maintenance_color_text3));
        }
        //时间
        holder.maintenance_tv_time.setText(DateTimeUtils.dateToYearMonthDayHour(new Date(flow.getOperate_time())));
        //状态
        holder.tv_state.setText(states[flow.getNext_status()]);
//        public String[] states = {"", "等待接单", "等待开始", "正在巡检", "正常结束", "异常结束", "逾期终止", "逾期未巡", "逾期结束"};
        switch (flow.getNext_status()+"") {
            case "1":// 派单
                if(flow.getOfficers()!=null){
                    for (int i = 0; i < flow.getOfficers().size(); i++) {
                        if(flow.getOfficers().get(i).getC_status().equals("1")){
                            holder.maintenance_tv_contents.setText("执行人: " +flow.getOfficers().get(i).getC_officer());
                        }else {
                            holder.maintenance_tv_name.setText("派单人员: " +flow.getOfficers().get(i).getC_officer());
                        }
                    }
                }
                holder.maintenance_tv_contents.setVisibility(View.VISIBLE);
                holder.maintenance_tv_name.setVisibility(View.VISIBLE);
                break;
            case "2":// 已接单
                holder.maintenance_tv_name.setVisibility(View.VISIBLE);
                holder.maintenance_tv_name.setText("执行人: " +flow.getOperator());
                holder.maintenance_tv_contents.setVisibility(View.INVISIBLE);
                break;
            case "3":// 开始巡检
            case "4":// 已完成
                holder.maintenance_tv_name.setVisibility(View.VISIBLE);
                holder.maintenance_tv_name.setText("执行人: " +flow.getOperator());
                holder.maintenance_tv_contents.setVisibility(View.INVISIBLE);
                break;
            case "5":// 异常结束
                holder.maintenance_tv_name.setVisibility(View.VISIBLE);
                holder.maintenance_tv_name.setText("异常原因: " +reason);
                holder.maintenance_tv_contents.setVisibility(View.INVISIBLE);
                break;
            default:
                holder.maintenance_tv_name.setVisibility(View.INVISIBLE);
                holder.maintenance_tv_contents.setText("执行人: " +flow.getOperator());
                holder.maintenance_tv_contents.setVisibility(View.VISIBLE);
                break;
        }

    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        ConstraintLayout maintenance_cl_maintenace;
        ImageView iv_progress;
        View view_dotted_line_bottom;
        TextView tv_state;
        TextView maintenance_tv_time;
        TextView maintenance_tv_name;
        TextView maintenance_tv_contents;
        ViewHolder(View itemView) {
            super(itemView);
            maintenance_cl_maintenace = itemView.findViewById(R.id.maintenance_cl_maintenace);
            iv_progress = itemView.findViewById(R.id.maintenance_iv_progress);
            view_dotted_line_bottom = itemView.findViewById(R.id.maintenance_view_dotted_line_bottom);
            tv_state = itemView.findViewById(R.id.maintenance_tv_state);
            maintenance_tv_time = itemView.findViewById(R.id.maintenance_tv_time);
            maintenance_tv_name = itemView.findViewById(R.id.maintenance_tv_name);
            maintenance_tv_contents = itemView.findViewById(R.id.maintenance_tv_contents);
        }
    }

}